from flask import Flask, redirect, render_template, request, url_for


class Server:
    def __init__(self, name, shop):
        self.shop = shop
        self.app = Flask(name)
        self.register_routes()

    def register_routes(self):
        @self.app.route("/")
        def index():
            return render_template("index.html")

        @self.app.route("/create_truck", methods=["GET"])
        def create_truck():
            return render_template("create_vehicle.html", type="truck")

        @self.app.route("/create_car", methods=["GET"])
        def create_car():
            return render_template("create_vehicle.html", type="car")

        @self.app.route("/create_truck", methods=["POST"])
        def add_truck():
            self.shop.add_truck()
            return redirect(url_for("show_vehicles"))

        @self.app.route("/create_car", methods=["POST"])
        def add_car():
            self.shop.add_car()
            return redirect(url_for("show_vehicles"))

        @self.app.route("/show_vehicles", methods=["GET"])
        def show_vehicles():
            return render_template("list.html", journal=self.shop.show_catalog())

        @self.app.route("/edit_vehicle/<int:inventory_number>", methods=["GET"])
        def edit_vehicle(inventory_number):
            return render_template(
                "edit_vehicle.html",
                vehicle_to_edit=self.shop.get_by_number(inventory_number),
                type=self.shop.get_by_number(inventory_number).type,
                num=inventory_number,
            )

        @self.app.route("/edit_vehicle/<int:inventory_number>", methods=["POST"])
        def confirm_edit_vehicle(inventory_number):
            print(request.form)
            print(inventory_number)
            self.shop.edit_car(inventory_number)
            return redirect(url_for("show_vehicles"))

        @self.app.route("/dump")
        def dump():
            self.shop.save()
            return render_template("success.html")

        @self.app.route("/load")
        def load():
            self.shop.load()
            return redirect(url_for("show_vehicles"))

        @self.app.route("/clear")
        def clear():
            self.shop.clear_all()
            return redirect(url_for("show_vehicles"))

    def run(self):
        self.app.run()


if __name__ == "__main__":
    print(dir(render_template))
